package ranab.server.ftp.gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import ranab.gui.GuiUtils;
import ranab.server.gui.ServerPane;
import ranab.server.ServerEngine;
import ranab.server.ftp.FtpServer;

/**
 * FTP server admin panel.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>.
 */
public 
class FtpPanel extends JPanel 
               implements ServerPane, TreeSelectionListener {
    
    private FtpTree  mjFtpTree  = null;
    private JPanel mjFtpPane    = null;
    
    /** 
     * Creates new form MyFtpPanel 
     */
    public FtpPanel() {
        initComponents ();
    }

    /** 
     * This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
        
        // top level
        setLayout(new BorderLayout());
        JSplitPane jSplitPane = new JSplitPane();
        jSplitPane.setDividerSize(2);
        add(jSplitPane, BorderLayout.CENTER);
        
        // left pane
        mjFtpTree = new FtpTree();
        mjFtpTree.addTreeSelectionListener(this);
        JScrollPane custPane = new JScrollPane(mjFtpTree, 
                                               JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                               JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jSplitPane.setLeftComponent(custPane);
        
        // right pane
        mjFtpPane = new JPanel();
        jSplitPane.setRightComponent(mjFtpPane);
        mjFtpPane.add(mjFtpTree.getRootPanel());
        
        jSplitPane.setDividerLocation(100);
    }
    
    /**
     * Handle tree selection
     */
    public void valueChanged(TreeSelectionEvent e) {
        JPanel dispPane = mjFtpTree.getSelectedPanel();
        if(dispPane != null) {
            GuiUtils.showNewPanel(mjFtpPane, dispPane);
        }
    }
     
    /**
     * Get server panel.
     */
    public JPanel getPanel() {
        return this;
    }
    
    /**
     * Get server represented by this panel.
     */
    public ServerEngine getServerEngine() {
        return mjFtpTree.getRootPanel().getServerEngine();
    }
     
    /**
     * Get server name.
     */
    public String getServerName() {
        return FtpServer.NAME; 
    }
}